package com.leyou.item.service.impl;

import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author CHERRY
 * @date 2018-09-01-15:09
 */
@Service
public class CategoryService implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> queryCategoryListByParentId(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        //通用mapper 父id查询 分类集合
        List<Category> categoryList = categoryMapper.select(category);
        return categoryList;
    }

    @Override
    public List<Category> queryBrandCategoryByBid(Long bid) {
        List<Category> categories = categoryMapper.selectBrandCategoryByBid(bid);
        return categories;
    }

    @Override
    public List<String> queryCategoryNamesByIds(List<Long> ids) {
        //继承SelectByIdlistMapper
        List<Category> categories = this.categoryMapper.selectByIdList(ids);
        List<String> names = categories.stream().map(category -> category.getName()).collect(Collectors.toList());
        return names;
    }

    @Override
    public List<Category> queryCategoryByCid(Long cid) {
        List<Category> categories = new ArrayList<>();
        Category category3 = this.categoryMapper.selectByPrimaryKey(cid);
        Category category2 = this.categoryMapper.selectByPrimaryKey(category3.getParentId());
        Category category1 = this.categoryMapper.selectByPrimaryKey(category2.getParentId());
        categories.add(category1);
        categories.add(category2);
        categories.add(category3);
        return categories;
    }
}
